package routes

import (
	"jianianhua-server/config"
	"jianianhua-server/handlers"
	"jianianhua-server/middleware"
	"jianianhua-server/services"

	"github.com/gin-gonic/gin"
)

func SetupRoutes(router *gin.Engine) {
	// 初始化服务
	cfg := config.LoadConfig()
	wechatService := services.NewWechatService(cfg)
	
	// 初始化处理器
	authHandler := handlers.NewAuthHandler(wechatService)
	productHandler := handlers.NewProductHandler()
	appointmentHandler := handlers.NewAppointmentHandler()
	bannerHandler := handlers.NewBannerHandler()
	userHandler := handlers.NewUserHandler()

	// API版本组
	v1 := router.Group("/api/v1")
	{
		// 认证相关路由
		auth := v1.Group("/auth")
		{
			auth.POST("/wechat/login", authHandler.WechatLogin)
			auth.PUT("/user/info", middleware.AuthMiddleware(wechatService), authHandler.UpdateUserInfo)
			auth.GET("/user/info", middleware.AuthMiddleware(wechatService), authHandler.GetUserInfo)
			auth.POST("/upload-avatar", middleware.AuthMiddleware(wechatService), authHandler.UploadAvatar)
			auth.DELETE("/user/delete", middleware.AuthMiddleware(wechatService), authHandler.DeleteUser)
		}

		// 商品相关路由
		products := v1.Group("/products")
		{
			products.GET("", productHandler.GetProducts)
			products.GET("/:id", productHandler.GetProduct)
			// 管理员商品管理接口
			products.POST("", middleware.AuthMiddleware(wechatService), productHandler.CreateProduct)
			products.PUT("/:id", middleware.AuthMiddleware(wechatService), productHandler.UpdateProduct)
			products.DELETE("/:id", middleware.AuthMiddleware(wechatService), productHandler.DeleteProduct)
			// 图片上传接口
			products.POST("/upload", middleware.AuthMiddleware(wechatService), productHandler.UploadImage)
		}


		// 预约配置相关路由（管理员接口）
		configs := v1.Group("/appointment-configs")
		{
			configs.POST("", appointmentHandler.CreateAppointmentConfig)
			// 注意：具体路由必须放在参数路由之前，避免路由冲突
			configs.GET("/available-dates", appointmentHandler.GetAvailableDates)
			// 美甲师人数设置路由（必须在 :date 路由之前）
			configs.POST("/staff-count", middleware.AuthMiddleware(wechatService), appointmentHandler.SetGlobalStaffCount)
			configs.GET("/staff-count", appointmentHandler.GetGlobalStaffCount)
			// 取消指定日期的所有预约（必须在 :date 路由之前）
			configs.POST("/cancel-appointments", middleware.AuthMiddleware(wechatService), appointmentHandler.CancelAppointmentsByDate)
			// 请假状态相关路由（必须在 :date 路由之前）
			configs.POST("/toggle-leave", middleware.AuthMiddleware(wechatService), appointmentHandler.ToggleLeaveStatus)
			// 参数路由放在最后
			configs.PUT("/:date", appointmentHandler.UpdateAppointmentConfig)
			configs.GET("/:date", appointmentHandler.GetAppointmentConfig)
			configs.PUT("/:date/leave", middleware.AuthMiddleware(wechatService), appointmentHandler.ToggleLeaveStatus)
			configs.GET("/:date/leave", appointmentHandler.GetLeaveStatus)
			// 已预约时间段查询路由
			configs.GET("/:date/booked-slots", appointmentHandler.GetBookedTimeSlots)
		}

		// 预约相关路由
		appointments := v1.Group("/appointments")
		appointments.Use(middleware.AuthMiddleware(wechatService))
		{
			appointments.POST("", appointmentHandler.CreateAppointment)
			appointments.GET("", appointmentHandler.GetAppointments)
			appointments.GET("/:id", appointmentHandler.GetAppointmentDetail)
			appointments.PUT("/:id/cancel", appointmentHandler.CancelAppointment)
			appointments.PUT("/:id/status", appointmentHandler.UpdateAppointmentStatus)
			appointments.PUT("/:id/estimated-time", appointmentHandler.UpdateAppointmentEstimatedTime)
		}

		// 用户设置相关路由
		v1.GET("/settings/user", handlers.GetUserSettings) // 公开接口，获取用户设置字典

		// 轮播图相关路由
		banners := v1.Group("/banners")
		{
			banners.GET("", bannerHandler.GetBanners) // 公开接口，获取启用的轮播图
		}

		// 轮播图管理路由（需要管理员权限）
		bannerAdmin := v1.Group("/banners")
		bannerAdmin.Use(middleware.AuthMiddleware(wechatService))
		{
			bannerAdmin.POST("", bannerHandler.CreateBanner)
			bannerAdmin.GET("/:id", bannerHandler.GetBanner)
			bannerAdmin.PUT("/:id", bannerHandler.UpdateBanner)
			bannerAdmin.DELETE("/:id", bannerHandler.DeleteBanner)
		}

		// 用户管理路由（需要管理员权限）
		users := v1.Group("/users")
		users.Use(middleware.AuthMiddleware(wechatService))
		{
			users.GET("", userHandler.GetUsers)
			users.GET("/:id", userHandler.GetUser)
			users.PUT("/:id/blacklist", userHandler.UpdateUserBlacklist)
			users.PUT("/:id/staff", userHandler.ToggleStaffStatus)
			users.GET("/staff/list", userHandler.GetStaffList)
		}

		// 用户拉黑状态检查路由（公开接口）
		v1.GET("/users/check-blacklist", middleware.AuthMiddleware(wechatService), userHandler.CheckUserBlacklist)
	}

	// 健康检查
	router.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"status":  "ok",
			"message": "服务运行正常",
		})
	})
}
